import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('../layout/Layout.vue'),
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '仪表盘', icon: 'Odometer'}
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('../views/user/UserList.vue'),
        meta: { title: '用户管理', icon: 'User', roles: ['1'] }
      },
      {
        path: 'ai-research',
        name: 'AIResearch',
        component: () => import('../views/research/AIResearch.vue'),
        meta: { title: '行业研究', roles: ['2'] }
      },
      // 添加新的AI聊天研究路由
      {
        path: 'ai-chat',
        name: 'AIChat',
        component: () => import('../views/research/AIChatResearch.vue'),
        meta: { title: 'AI对话研究', icon: 'ChatDotRound', roles: ['2'] }
      },
      {
        path: 'industry-domain',
        name: 'IndustryDomain',
        component: () => import('../views/industry/IndustryDomainList.vue'),
        meta: { title: '行业领域管理', icon: 'Menu', roles: ['1'] }
      },
      {
        path: 'history',
        name: 'History',
        component: () => import('../views/history/HistoryList.vue'),
        meta: { title: '历史记录', icon: 'List', roles: ['2']  }
      },
      {
        path: 'profile/index',
        name: 'Profile',
        component: () => import('../views/profile/UserProfile.vue'),
        meta: { title: '个人信息', icon: 'User' }
      }
    ]
  },
  {
    path: '/finance',
    name: 'Finance',
    component: () => import('../layout/Layout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: 'health-dashboard',
        name: 'FinanceHealthDashboard',
        component: () => import('../views/finance/FinanceHealthDashboard.vue'),
        meta: {
          title: '财务健康评估',
          requiresAuth: true,
          roles: ['1', '2'] // 管理员和普通用户都可以访问
        }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 检查路由是否需要认证
  if (to.meta.requiresAuth !== false) {
    // 如果用户未登录，重定向到登录页
    if (!userStore.isLoggedIn) {
      next({ name: 'Login', query: { redirect: to.fullPath } })
      return
    }

    // 检查角色权限
    if (to.meta.roles && to.meta.roles.length > 0) {
      const userRoleId = userStore.userInfo.roleId
      if (!userRoleId || !to.meta.roles.includes(userRoleId)) {
        // 如果用户没有权限访问该页面，重定向到首页
        next({ name: 'Dashboard' })
        return
      }
    }
  }

  // 如果用户已登录且访问登录页，重定向到首页
  if (to.name === 'Login' && userStore.isLoggedIn) {
    next({ name: 'Dashboard' })
    return
  }

  next()
})

export default router
